/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof_trab1;

import Downloader.Downloader;
import java.util.Random;
import dbaccess.Coleccao;
import dbaccess.Ficheiro;
import java.util.ArrayList;
import java.util.Date;

public class ExecutorDownloader implements Runnable {

    private ResizeableSemaphore rs;
    ArrayList<String> links;
    //****************************************************************************************************************************
    //isto terá que vir da bd
    Integer id = 9;
    String dominio = "http://www.supertangas.com/";
    String ficheiro = "index.html";
    String estado = "parse";
    long tamanhosite = 100000;
    String caminhosite = "http://www.heu.com/";
    String caminholocal = "C:\\Users\\Helder\\Desktop\\";
    String tipo = "html";
    Integer niveis = 0;
    Date actualizado = new Date();
    Ficheiro dbObj = new Ficheiro(id, dominio, ficheiro, estado, tamanhosite, caminhosite, caminholocal, tipo, niveis, actualizado);
    //String url, String pathLocal, int nivelMax
    private Downloader dw = new Downloader();
    //****************************************************************************************************************************

    public ExecutorDownloader(ResizeableSemaphore sem) throws InterruptedException {
        this.rs = sem;
        this.rs.acquire();
    }

    @Override
    public void run() {
        Random generator = new Random();
        try {
            long downloading = generator.nextInt(100000) + 1;
            System.out.println("Downloader: " + this.toString() + " started...");

            Coleccao listOfFiles = new Coleccao();
            listOfFiles.getListaIncompletos(null);

            BandwithAllocation.getInstance().addDownload(downloading);
            //aqui chamar o Downloader om os dados do site
//            dw.Downloader(dbObj.getDominio(), dbObj.getCaminholocal(), 3);
            dw.Downloader(dbObj, 3);
            dw.DownloadFile();


            Thread.sleep(2000);
            BandwithAllocation.getInstance().removeDownload(downloading);

            System.out.println("Downloader: " + this.toString() + " finished.");

            this.rs.release();
        } catch (InterruptedException ex) {
        }
    }
}
